import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

public class LevelCase extends JPanel {

	private static final long serialVersionUID = -4700844863726612361L;

	protected Integer mValue;

	public LevelCase(Integer aCaseValue) {
		this(aCaseValue, true);
	}

	public LevelCase(Integer aCaseValue, boolean isMouseSensitive) {
		this.mValue = aCaseValue;
		this.updateColor();
		this.setSize(new Dimension(15, 15));
		this.setPreferredSize(new Dimension(15, 15));

		if (isMouseSensitive) {
			this.addMouseEvents();
		}
	}

	public void addMouseEvents() {

		this.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				// Si c'est un click GAUCHE
				if (e.getButton() == MouseEvent.BUTTON1) {
					// Augmentation de la valeur
					LevelCase.this.setValue(LevelCase.this.getValue() + 1);
				}
				// Si c'est un click DROIT
				else if (e.getButton() == MouseEvent.BUTTON3) {
					// Décrémentation de la valeur
					LevelCase.this.setValue(LevelCase.this.getValue() - 1);
				}
				// Si c'est un click MOLETTE
				else if (e.getButton() == MouseEvent.BUTTON2) {
					// Remise à zéro de la valeur
					LevelCase.this.setValue(0);
				}
			}
		});
	}

	public LevelCase() {
		this(0);
	}

	public Integer getValue() {
		return mValue;
	}

	public void setValue(Integer mValue) {
		this.mValue = mValue;
		this.updateColor();
	}

	public Color getCaseColor() {

		Color color = Color.red;

		switch (this.mValue) {
		case 0:
			color = Color.white;
			break;

		case 1:
			color = Color.black;
			break;
			
		case 2:
			color = Color.blue;
			break;			

		case 3:
			color = Color.yellow;
			break;
			
		case 4:
			color = Color.pink;
			break;
			
		case 5:
			color = Color.orange;
			break;

		case -1:
			color = Color.green;
			break;

		case -2:
			color = Color.green.darker();
			break;

		case -3:
			color = Color.green.darker().darker();
			break;

		case -4:
			color = Color.green.darker().darker().darker();
			break;

		default:
			color = Color.red;
		}

		return color;
	}

	public void updateColor() {
		this.setBackground(this.getCaseColor());
	}
}
